package dao;  
import java.sql.ResultSet;  
import java.sql.SQLException;  
import java.util.ArrayList;  
import java.util.List;  
import entity.User;  
import util.DBconn;  
public class UserDaoImpl implements UserDao{  
public boolean register(User user) {  
boolean flag = false;  
      DBconn.init();  
int i =DBconn.addUpdDel("insert into user(name,pwd,sex,home,info) " +  
"values('"+user.getName()+"','"+user.getPwd()+"','"+user.getGen()+"','"+user.getEmail()+"','"+user.getTelephone()+"')");  
if(i>0){  
          flag = true;  
      }  
      DBconn.closeConn();  
return flag;  
  }  
public boolean login(String name, String pwd) {  
boolean flag = false;  
try {  
              DBconn.init();  
              ResultSet rs = DBconn.selectSql("select * from user where name='"+name+"' and pwd='"+pwd+"'");  
while(rs.next()){  
if(rs.getString("name").equals(name) && rs.getString("pwd").equals(pwd)){  
                      flag = true;  
                  }  
              }  
              DBconn.closeConn();  
      } catch (SQLException e) {  
          e.printStackTrace();  
      }  
return flag;  
  }  
public List<User> getUserAll() {  
      List<User> list = new ArrayList<User>();  
try {  
          DBconn.init();  
          ResultSet rs = DBconn.selectSql("select * from user");  
while(rs.next()){  
              User user = new User();  
              user.setId(rs.getInt("id"));  
              user.setName(rs.getString("name"));  
              user.setPwd(rs.getString("pwd"));  
              user.setGen(rs.getString("gen"));  
              user.setEmail(rs.getString("email"));  
              user.setTelephone(rs.getString("telephone"));  
              list.add(user);  
          }  
          DBconn.closeConn();  
return list;  
      } catch (SQLException e) {  
          e.printStackTrace();  
      }  
return null;  
  }  
public boolean update(int id,String name, String pwd,String gen, String email,String telephone) {  
boolean flag = false;  
      DBconn.init();  
      String sql ="update user set name ='"+name  
              +"' , pwd ='"+pwd  
              +"' , gen ='"+gen  
              +"' , email ='"+email  
              +"' , telephone ='"+telephone+"' where id = "+id;  
int i =DBconn.addUpdDel(sql);  
if(i>0){  
          flag = true;  
      }  
      DBconn.closeConn();  
return flag;  
  }  
public boolean delete(int id) {  
boolean flag = false;  
      DBconn.init();  
      String sql = "delete  from user where id="+id;  
int i =DBconn.addUpdDel(sql);  
if(i>0){  
          flag = true;  
      }  
      DBconn.closeConn();  
return flag;  
  }  
}  